<template>
  <div class="course-info">
    <van-cell-group :style="styleOptions">
      <!-- 课程图片 -->
      <van-cell class="course-image">
        <img :src="course.courseImgUrl" alt="">
      </van-cell>
      <!-- 课程描述 -->
      <van-cell class="course-description">
        <!-- 课程名称 -->
        <h2 v-text="course.courseName"></h2>
        <!-- 课程概述 -->
        <p v-text="course.previewFirstField"></p>
        <!-- 课程销售信息 -->
        <div class="course-sale-info">
            <p class="course-price">
                <span class="discounts">￥{{course.discounts }}</span>
                <span class="price">￥{{course.price }}</span>
            </p>
            <span class="tag">{{ course.sales}}人已购</span>
            <span class="tag">每周三、五更新</span>
        </div>
      </van-cell>
      <!-- 课程详细内容 -->
      <van-cell class="course-detail">
        <van-tabs sticky scrollspy>
          <van-tab title="详情">
            <!-- 课程详情在后台是通过富文本编辑器生成的 内容为html文本 -->
            <div v-html="course.courseDescription"></div>
          </van-tab>
          <van-tab title="内容">
            <course-section
            v-for="item in courseSectionList" :key="item.id"
            :section-data="item">
            </course-section>
          </van-tab>
        </van-tabs>
      </van-cell>
    </van-cell-group>
    <!-- 底部支付功能 -->
    <van-tabbar v-if="!course.isBuy">
        <div class="price">
          <span v-text="course.discountsTag"></span>
          <span class="discounts">￥{{course.discounts }}</span>
          <span class="price">￥{{course.price }}</span>
        </div>
        <van-button type="primary" @click="handlePay">立即购买</van-button>
    </van-tabbar>
  </div>
</template>

<script>
import { Button, Tabbar, Tab, Tabs, Cell, CellGroup } from 'vant'
import CourseSection from './components/courseSection'
import { getCourseById, getSectionAndLesson } from '@/services/course'
export default {
  name: 'CourseInfo',
  components: {
    CourseSection,
    VanCell: Cell,
    VanCellGroup: CellGroup,
    VanButton: Button,
    VanTabbar: Tabbar,
    VanTab: Tab,
    VanTabs: Tabs
  },
  props: {
    courseId: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      // 课程信息
      course: [],
      // 课程章节信息
      courseSectionList: [],
      // 课程所有信息（支付功能前面的信息）
      styleOptions: {}
    }
  },
  created () {
    // 课程信息
    this.loadCourseById()
    // 课程章节信息
    this.SectionAndLesson()
  },
  methods: {
    async loadCourseById () {
      const { data } = await getCourseById({
        courseId: this.courseId
      })
      // console.log(data)
      this.course = data.content
      if (data.content.isBuy) {
        this.styleOptions.bottom = 0
      }
    },
    async SectionAndLesson () {
      const { data } = await getSectionAndLesson({
        courseId: this.courseId
      })
      // console.log(data)
      this.courseSectionList = data.content.courseSectionList
    },
    handlePay () {
      if (this.$store.state.user) {
        // 已登录跳转到支付页面
        this.$router.push({
          name: 'pay',
          params: {
            courseId: this.courseId
          }
        })
      } else {
        // 跳转到登录页
        this.$router.push({
          name: 'login',
          query: {
            redirect: this.$route.fullPath
          }
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
// 顶部图片设置
.van-cell{
  padding: 0%;
}
.course-image {
  height: 280px;
  }
// 中间描述区域
.course-description {
  padding: 10px 20px;
  height: 150px;
}
.course-description h2 {
  padding: 0;
}
.course-sale-info{
  display: flex;
}
.course-sale-info .course-price{
  flex: 1;
  margin: 0;
}
.discounts{
    color:#ff7452;
    font-size: 22px !important;
    font-weight: 600;
}
.course-sale-info .tag{
    background-color:#f8f9fa;
    font-size: 12px;
    font-weight: 600;
    color: #666;
    margin-left: 10px;
    padding: 7px;
    line-height: 15px;
    border-radius: 5px;

}
// 底部支付样式
.van-cell-group{
  width: 100%;
  position: fixed;
  top:0;
  bottom: 50px;
  overflow-y: auto;
}
.van-tabbar{
    line-height: 50px;
    padding: 0 15px;
    box-sizing: border-box;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.price span{
    font-size: 16px;
}
.van-button{
    width: 45%;
    height: 80%;
}
</style>
